-- brokenRobot
-- Created by panyl Nov/25/2016
-- 机器人的残骸

-- 初始化
function initBrokenRobot(self)
    self.item:removeAllChildren();

    local class = self.gridData.class;
    local element = self.gridData.element;
    local buildingInfo = BuildingBonusM.queryBuildingView(class);

    if not buildingInfo then
        error("获取不到建筑查看配置，元素类型：" .. class);
        return;
    end

    local elemDbase = DungeonM.queryElement(element, "dbase");

    local function onClicked(sender, eventType)
        if eventType == ccui.TouchEventType.began then
            if self.gridData.state == GRID_STATE_DISABLE then
                -- 已经失效，不处理
                return;
            end

                 -- 更新建筑访问
            AudioM.playFx("ui_open1");
            self:updateBuildingVisit();

            if not DungeonM.isAllMonsterDead() then
                -- 需要击杀所有怪物方可拾取
                self:openBuildingView(nil, -20, buildingInfo["dialog2"], getLocStr("btn_text_leave"));
                return;
            end

            -- 修复
            local function repair()
                local pos = self.gridData:getPos();
                DungeonActionM.go("take_broken_robot", pos, 1);
            end

            -- 翻找
            local function take()
                local pos = self.gridData:getPos();
                DungeonActionM.go("take_broken_robot", pos, 0);
            end

            -- 还未获得机器人
            if elemDbase["robot_id"] and SkyRobotM.getAmount(elemDbase["robot_id"]) <= 0 then
                self:openBuildingView2(take, repair, buildingInfo["view_text"], buildingInfo["view_text2"], buildingInfo["dialog"]);

                return;
            end

            self:openBuildingView(take);
        end
    end

    local iconPath = getLevelItemIconPath(buildingInfo["icon"]);
    self.icon = ccui.ImageView:create(iconPath);
    self.icon:setTouchEnabled(true);
    self.icon:addTouchEventListener(onClicked);
    self.item:addChild(self.icon);

    -- 发光
    if self.gridData.need_shining == 1 then
        shiningEffect(self.icon, 70, 100);
    end
end

